﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuoiHoi.Business;
using CuoiHoi.Entities;
using System.IO;

namespace CuoiHoi.Admin
{
    public partial class ucQuanLyNhanVien : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
            LoadCbbChucVu();
        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {

            if (FileUpload1.HasFile)
            {
                string filename = "";
                foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
                {
                    filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + uploadedFile.FileName;
                    uploadedFile.SaveAs(Path.Combine(Server.MapPath("~/images/"), filename));
                    BNhanVien.Insert(new ENhanVien
                    {
                        AnhDaiDien = filename,
                        idChucVu = int.Parse(ddChucVu.SelectedValue.ToString()),
                        MatKhau = Router.MaHoaMD5(txtMatKhau.Text),
                        SoDienThoai = txtSoDienThoai.Text,
                        TenDangNhap = txtTaiKhoan.Text,
                        TenNhanVien = txtTenNhanVien.Text
                    });
                }
                LoadData();
                Session["action"] = 1;
            }
        }

        void LoadData()
        {
            grvNhanVien.AutoGenerateColumns = false;
            grvNhanVien.DataSource = BNhanVien.SelectAll();
            grvNhanVien.DataBind();
        }

        void LoadCbbChucVu()
        {
            ddChucVu.DataSource = BChucVu.SelectAll();
            ddChucVu.DataTextField = "TenChucVu";
            ddChucVu.DataValueField = "idChucVu";
            ddChucVu.DataBind();
        }

        protected void grvNhanVien_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BNhanVien.Delete(int.Parse(grvNhanVien.DataKeys[e.RowIndex].Value.ToString()));
            Session["action"] = 3;
            LoadData();
        }

        protected void grvNhanVien_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvNhanVien.EditIndex = -1;
            grvNhanVien.DataBind();
        }

        protected void grvNhanVien_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvNhanVien.EditIndex = e.NewEditIndex;
            grvNhanVien.DataBind();
        }

        protected void grvNhanVien_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DropDownList dd = grvNhanVien.Rows[e.RowIndex].FindControl("cbbChucVu") as DropDownList;
            BNhanVien.ADUpdate(new ENhanVien
            {
                idNhanVien=int.Parse(grvNhanVien.DataKeys[e.RowIndex].Value.ToString()),
                TenNhanVien = ((TextBox)grvNhanVien.Rows[e.RowIndex].FindControl("txtTenNhanVien")).Text,
                SoDienThoai = ((TextBox)grvNhanVien.Rows[e.RowIndex].FindControl("txtSoDienThoai")).Text,
                idChucVu=int.Parse(dd.SelectedValue.ToString())
            });
            grvNhanVien.EditIndex = -1;
            Session["action"] = 2;
            LoadData();
        }

        protected void grvNhanVien_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {
                    DropDownList cbbChuVu = (DropDownList)e.Row.FindControl("cbbChucVu");
                    if (cbbChuVu != null)
                    {
                        cbbChuVu.DataSource = BChucVu.SelectAll();
                        cbbChuVu.DataTextField = "TenChucVu";
                        cbbChuVu.DataValueField = "idChucVu";
                        cbbChuVu.DataBind();
                    }           

                    LinkButton btDelete = (LinkButton)e.Row.Cells[8].Controls[0];
                    if (btDelete != null)
                    {
                        btDelete.Attributes.Add("OnClick", "javascript:return confirm('Bạn có muốn xóa')");
                    }
                }
                catch { }
            }
        }

    }
}